<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据统计</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }

        .controls {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 30px;
        }

        label {
            margin-right: 10px;
            font-weight: 500;
        }

        input[type="month"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #2980b9;
        }

        table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        th,
        td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }

        th {
            background-color: #3498db;
            color: white;
            font-weight: 500;
        }

        tr:last-child td {
            border-bottom: none;
        }

        tr:nth-child(even) {
            background-color: #f8f8f8;
        }

        .no-data-message {
            text-align: center;
            padding: 20px;
            font-style: italic;
            color: #777;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数据统计</h1>

        <div class="controls">
            <label for="month">选择月份:</label>
            <input type="month" id="month" name="month">
            <button id="fetchData">查询</button>
        </div>

        <table>
            <thead>
                <tr>
                    <th>终态日期</th>
                    <th>商户号</th>
                    <th>运营商</th>
                    <th>产品面额</th>
                    <th>状态</th>
                    <th>总笔数</th>
                    <th>订单总金额</th>
                    <th>客户结算总金额</th>
                    <th>供应商结算总金额</th>
                </tr>
            </thead>
            <tbody id="dataTable">
            </tbody>
        </table>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const currentDate = new Date();
            const currentMonth = currentDate.toISOString().slice(0, 7);
            document.getElementById('month').value = currentMonth;

            const carrierMap = {
                'CM': '移动',
                'CT': '电信',
                'CU': '联通',
                'BN': '广电'
            };

            const statusMap = {
                '0': '成功',
                '3': '返销'
            };
            function toYuan(amount, unit) {
                const divisor = unit === 'cent' ? 100 : 10000;
                const value = (amount / divisor).toFixed(2);
                return value;
            }

            document.getElementById('fetchData').addEventListener('click', function () {
                const selectedMonth = document.getElementById('month').value.replace('-', '');
                fetchData(selectedMonth);
            });

            function fetchData(month) {
                const formData = new FormData();
                formData.append('month', month);

                fetch('https://dytest.zhaoshujie.top/console/staticsAmtQuery', {
                // fetch('http://localhost:9999/console/staticsAmtQuery', {
                    method: 'POST',
                    body: formData,
                })
                    .then(response => response.json())
                    .then(data => {
                        if (data.Code === '0000') {
                            displayData(data.Data);
                        } else {
                            alert('Error: ' + data.Msg);
                        }
                    })
                    .catch(error => console.error('Error fetching data:', error));
            }

            function displayData(data) {
                const dataTable = document.getElementById('dataTable');
                dataTable.innerHTML = '';
                if (data && Array.isArray(data) && data.length > 0) {
                    data.forEach(item => {
                        const row = document.createElement('tr');
                        const carrierName = carrierMap[item.carrierCode] || item.carrierCode;
                        const statusName = statusMap[item.orderStatus] || item.orderStatus;
                        row.innerHTML = `
                            <td>${item.finishDate}</td>
                            <td>${item.appId}</td>
                             <td>${carrierName}</td>
                           <td>${toYuan(item.productDenomination, 'cent')}</td>
                            <td>${statusName}</td>
                            <td>${item.totalNum}</td>
                           <td>${toYuan(item.totalAmt, 'cent')}</td>
                        <td>${toYuan(item.totalContractAmt, 'mill')}</td>
                        <td>${toYuan(item.totalSupplierAmt, 'mill')}</td>
                        `;

                        dataTable.appendChild(row);
                    });
                } else {

                    dataTable.innerHTML = `
                        <tr>
                            <td colspan="9" class="no-data-message">当月暂无数据</td>
                        </tr>
                    `;
                }
            }
        });
    </script>
</body>

</html>